import { PEOPLE_TYPE } from '@/config/h5/index.js'
import API from '@/services/h5/product'
export default {
  namespaced: true,
  state: {
    productInfo: {
      productName: '',
      productId: ''
    },
    contractId: '', //订单id，如果是再修改则有值
    ids: [], //附件id，包含投被保人、受益人身份证和银行卡
    PEOPLE_TYPE,
    appPolicyHolderInfo: {
      // 保险人
      chnName: '', // 姓名
      id: '',
      sex: '',
      birthday: '',
      idType: '', // 证件类型
      idNo: '',
      mobile: '',
      // area: ['150000', '150100', '150102'],
      area: [],
      street: '',
      email: '',
      bankId: '',
      branchBankName: '', //分支行
      cardNo: '', // 银行卡号
      remarksHolder: '',

      isIdLongTerm: '', //证件是否长期有效,可用值:Y/N
      idEndDate: '', //证件有效期止期
      name: '',
      province: '',
      city: '',
      country: ''
    },
    appPolicyInsuredList: [
      {
        // 被保险人
        relationType: '', // 是投保人的
        idType: '', //证件类型
        chnName: '',
        id: '',
        idNo: '',
        sex: '',
        birthday: '',
        isIdLongTerm: '', //证件是否长期有效,可用值:Y/N
        idEndDate: '', // 证件有效期止期
        nationalityCode: '156', //国籍

        height: '',
        weight: '',
        marriageType: '',
        zipCode: '',
        area: [],
        street: '',
        annualIncome: '',
        isSocialSecurity: '', //是否有社保,可用值:Y/N
        name: '',
        province: '',
        city: '',
        country: ''
      }
    ],
    appPolicyBeneficiaryList: [
      {
        isLegalBeneficiary: '',
        relationType: '', // 与被保险人的关系
        name: '', // 姓名
        chnName: '',
        id: '',
        idType: '', // 证件类型
        idNo: '',
        isIdLongTerm: '', //证件是否长期有效,可用值:Y/N
        idEndDate: '', //证件有效期止期
        beneficialOrder: '', // 受益人顺序
        beneficialRatio: '' // 受益人比列
      }
    ],
    riskList: [
      {
        isShow: false,
        riskId: '',
        insurancePeriodId: '',
        payPeriodId: '',
        paymentWay: '',
        sumAssured: '',
        premium: '',
        partIds: [],
        prFlag: 'M',
        partId: ''
      }
    ],
    info: {
      agent: '',
      partTimerId: '',
      receiver: '',
      receiverMobile: '',
      reAddr: [],
      receiverAddress: '',
      receiverEmail: '',
      receiverCity: '',
      receiverCountry: '',
      receiverProvince: ''
    },
    policyFiles: []
  },
  getters: {
    productInfo: state => {
      return state.productInfo
    }
  },
  mutations: {
    // 初始化数据
    initData(state) {
      state.appPolicyHolderInfo = {
        // 保险人
        chnName: '', // 姓名
        sex: '',
        birthday: '',
        idType: '', // 证件类型
        idNo: '',
        mobile: '',
        // area: ['150000', '150100', '150102'],
        area: [],
        street: '',
        email: '',
        bankId: '',
        branchBankName: '', //分支行
        cardNo: '', // 银行卡号
        remarksHolder: '',

        isIdLongTerm: '', //证件是否长期有效,可用值:Y/N
        idEndDate: '', //证件有效期止期
        name: '',
        province: '',
        city: '',
        country: ''
      }
      state.appPolicyBeneficiaryList = [
        {
          isLegalBeneficiary: '',
          relationType: '', // 与被保险人的关系
          chnName: '', // 姓名
          idType: '', // 证件类型
          idNo: '',
          isIdLongTerm: '', //证件是否长期有效,可用值:Y/N
          idEndDate: '', //证件有效期止期
          beneficialOrder: '', // 受益人顺序
          beneficialRatio: '', // 受益人比列
          name: ''
        }
      ]
      state.appPolicyInsuredList = [
        {
          // 被保险人
          relationType: '', // 是投保人的
          idType: '', //证件类型
          chnName: '',
          idNo: '',
          sex: '',
          birthday: '',
          isIdLongTerm: '', //证件是否长期有效,可用值:Y/N
          idEndDate: '', // 证件有效期止期
          nationalityCode: '156', //国籍

          height: '',
          weight: '',
          marriageType: '',
          zipCode: '',
          area: [],
          street: '',
          annualIncome: '',
          isSocialSecurity: '', //是否有社保,可用值:Y/N
          name: '',
          province: '',
          city: '',
          country: '',
          mobile: '',
          email: ''
        }
      ]
      state.contractId = '' //订单id，如果是再修改则有值
      state.ids = [] //附件id，包含投被保人、受益人身份证和银行卡

      state.riskList = [
        {
          isShow: false,
          riskId: '',
          insurancePeriodId: '',
          payPeriodId: '',
          paymentWay: '',
          sumAssured: '',
          premium: '',
          partIds: [],
          prFlag: 'M',
          partId: '',
          carPolicyNo: ''
        }
      ]
      state.info = {
        agent: '',
        partTimerId: '',
        receiver: '',
        receiverMobile: '',
        reAddr: [],
        receiverAddress: '',
        receiverEmail: '',
        receiverCity: '',
        receiverCountry: '',
        receiverProvince: ''
      }
      state.policyFiles = []
    },
    // 设置产品信息
    setProductInfo(state, productInfo) {
      state.productInfo = productInfo
    },
    // 设置投保人信息
    setField(state, [prop, value]) {
      state.appPolicyHolderInfo[prop] = value
    },
    // 设置被保险人、受益人信息
    setArrayField(state, allvalue) {
      let [value, prop, data, index] = allvalue
      state[data][index][prop] = value
    },
    addArrayBeneficiary(state, obj) {
      state.appPolicyBeneficiaryList.push(obj)
    },
    addArrayPolicyInsured(state, obj) {
      state.appPolicyInsuredList.push(obj)
    },
    // 添加附件
    addFileInfoId(state, id) {
      state.ids.push(id)
    },
    // 设置订单号contractId
    setContractId(state, id) {
      state.contractId = id
    },
    // 受益人
    setArrayBenefical(state, arr) {
      state.appPolicyBeneficiaryList = arr
    },
    // 被保险人
    setArrayInsuredList(state, arr) {
      state.appPolicyInsuredList = arr
    },
    // 投保人
    setAppPolicyHolderInfo(state, obj) {
      state.appPolicyHolderInfo = obj
    },
    //
    setRiskList(state, arr) {
      state.riskList = arr
    },
    // 第三步配送
    setPolicyDistObj(state, obj) {
      state.info = obj
    },
    setPolicyDistProp(state, [prop, value]) {
      state.info[prop] = value
    },
    setPolicyFiles(state, arr) {
      state.policyFiles = arr
    }
  },
  actions: {
    // 获取产品详情信息
    async getProductDetailInfo({ commit, dispatch }, data) {
      const geObjArea = item => {
        item.area = [item.province, item.city, item.country]
        return item
      }
      const getArrArea = arr => {
        arr.map(item => {
          item.area = [item.province, item.city, item.country]
        })
        return arr
      }
      const getpolicy = item => {
        item.reAddr = [item.receiverProvince, item.receiverCity]
        return item
      }
      const getPolicyFiles = arr => {
        if (arr.length == 0) return []
        let data = []
        arr.forEach(item => {
          data.push({
            url: item.filePath,
            id: item.id
          })
        })
        return data
      }
      const getBefi = arr => {
        let res = []
        arr.map(item => {
          let obj = {
            isLegalBeneficiary: item.isLegalBeneficiary,
            relationType: item.relationType ? item.relationType : '', // 与被保险人的关系
            chnName: item.chnName ? item.chnName : '', // 姓名
            idType: item.idType ? item.idType : '', // 证件类型
            idNo: item.idNo ? item.idNo : '',
            isIdLongTerm: item.isIdLongTerm ? item.isIdLongTerm : '', //证件是否长期有效,可用值:Y/N
            idEndDate: item.idEndDate ? item.idEndDate : '', //证件有效期止期
            beneficialOrder: item.beneficialOrder ? item.beneficialOrder : '', // 受益人顺序
            beneficialRatio: item.beneficialRatio ? item.beneficialRatio : '', // 受益人比列
            name: item.name ? item.name : ''
          }
          res.push(obj)
        })
        return res
      }
      if (data.contractId) {
        commit('setContractId', data.contractId)
        const res = await API.getProductDetailInfo(data)
        if (res.data && res.data.code == 1000) {
          let productInfo = {
            productId: res.data.data.policyBase.productId,
            productName: res.data.data.policyBase.productName
          }
          commit('setProductInfo', productInfo)
          commit(
            'setAppPolicyHolderInfo',
            geObjArea(res.data.data.policyHolder)
          )
          commit(
            'setArrayInsuredList',
            getArrArea(res.data.data.policyInsureds)
          )
          commit(
            'setArrayBenefical',
            getBefi(res.data.data.policyBeneficiaries)
          )
          commit('setPolicyDistObj', getpolicy(res.data.data.policyBase))
          commit('setPolicyFiles', getPolicyFiles(res.data.data.policyFiles))
          await dispatch('getRiskList', res.data.data.policyRisks)
        }
      }
    },
    async getRiskList({ commit }, arr) {
      let data = []
      if (arr && arr.length !== 0) {
        arr.map((item, index) => {
          let obj = {
            isShow: false,
            riskId: item.riskId,
            insurancePeriodId: item.insurancePeriodId,
            payPeriodId: item.payPeriodId,
            paymentWay: item.payWayId,
            sumAssured: item.sumAssured,
            premium: item.premium,
            partIds: item.partIds ? item.partIds : [],
            prFlag: 'R',
            partId: item.partIds ? item.partIds.join(',') : ''
          }
          if (index === 0) {
            obj.prFlag = 'M'
          }
          data.push(obj)
        })
      } else {
        data = [
          {
            isShow: false,
            riskId: '',
            insurancePeriodId: '',
            payPeriodId: '',
            paymentWay: '',
            sumAssured: '',
            premium: '',
            partIds: [],
            prFlag: 'M',
            partId: ''
          }
        ]
      }
      commit('setRiskList', data)
    }
  }
}
